package com.photoeditor.demo.ui.widget.image.emoji;

import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.photoeditor.R;
import com.photoeditor.demo.app.BaseRecycleViewAdapter;
import com.photoeditor.demo.model.image.emoji.StickerCategoryBean;
import com.photoeditor.demo.model.subscribe.manager.AppVipManager;
import com.photoeditor.demo.ui.widget.image.FilterRecylerAdapter;

/**
 * Created by Administrator on 2017/9/23 0023.
 */

public class StickerButtonAdapter extends BaseRecycleViewAdapter<StickerCategoryBean> {

    private int mCheckedPosition = -1;

    private Drawable bgColor;
    private Drawable bgCheckColor;

    private int lastPosition = 0;//上次选中的position
    private boolean inited = true;

    @Override
    protected RecyclerView.ViewHolder createVHolder(ViewGroup parent, int viewType) {
        EmojiCheckableItem view = new EmojiCheckableItem(parent.getContext());
        int mItemHeight = (int) parent.getContext().getResources().getDimension(R.dimen.image_eidt_emoji_bar_height);
        int mItemWidth = (int) parent.getContext().getResources().getDimension(R.dimen.image_eidt_emoji_bar_width);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(mItemWidth, mItemHeight);
        view.setLayoutParams(lp);
        return new StickerButtonViewHolder(view);
    }

    @Override
    protected void showItemView(RecyclerView.ViewHolder holder, int position) throws PackageManager.NameNotFoundException {
        EmojiCheckableItem item = (EmojiCheckableItem) holder.itemView;
        item.setBgCheckDrawable(bgColor, bgCheckColor);
        StickerCategoryBean categoryBean = mData.get(position);
        item.setTag(categoryBean.getPkgName());
        item.setPackageName(categoryBean);
        if (mCheckedPosition == position) {
            item.setChecked(true);
        } else {
            item.setChecked(false);
        }
        item.setVip(categoryBean.isBuy() && !AppVipManager.isVip());

        //setCheckedPosition（）的代码刷新单个item
        if (inited && position == lastPosition){  //初次进入显示当前position为选中状态
            inited = false;
            lastPosition = position;
            item.setChecked(true);
        }else if (lastPosition != position){
            item.setChecked(false);
        }else{
            item.setChecked(true);
        }
    }

    public int setCheckedPosition(String pkgName) {
        int position = -1;
        for (int i = 0; i < mData.size(); i++) {
            if (TextUtils.equals(mData.get(i).getPkgName(), pkgName)) {
                position = i;
                break;
            }
        }
        mCheckedPosition = position;
        notifyDataSetChanged();
        return position;
    }

    public void setCheckedPosition(int position, RecyclerView recyclerView) {
        mCheckedPosition = position;
//        notifyDataSetChanged();
        if (position == -1) {
//            notifyDataSetChanged();
            lastPosition = 0;
            inited = true;
        }else {
            //配合刷新单个item
            if (lastPosition != position) {
                StickerButtonViewHolder holder1 = (StickerButtonViewHolder) recyclerView.findViewHolderForLayoutPosition(lastPosition);
                if (holder1 != null) {
                    EmojiCheckableItem item = (EmojiCheckableItem) holder1.itemView;
                    if (item != null) {//还在屏幕里
                        item.setChecked(false);  //取消选中
                    } else {
                        notifyItemChanged(lastPosition);
                    }
                }
                //设置新Item的勾选状态
                lastPosition = position;
                StickerButtonViewHolder holder2 = (StickerButtonViewHolder) recyclerView.findViewHolderForLayoutPosition(lastPosition);
                if (holder2 !=null) {
                    EmojiCheckableItem item2 = (EmojiCheckableItem) holder2.itemView;
                    if (item2 != null) {
                        item2.setChecked(true);  //设置选中
                    }
                }
            }
        }
    }

    public String getLastSelectPkgName() {
        return mData.get(mCheckedPosition >= 0 ? mCheckedPosition : 0).getPkgName();
    }

    public void setBgCheckDrawable(Drawable bgColor, Drawable bgCheckColor) {
        this.bgColor = bgColor;
        this.bgCheckColor = bgCheckColor;
    }

    public boolean containsPkgName(String pkgName) {
        if (mData == null || mData.isEmpty()) {
            return false;
        }
        for (StickerCategoryBean categoryBean : mData) {
            if (TextUtils.equals(categoryBean.getPkgName(), pkgName)) {
                return true;
            }
        }
        return false;
    }

    public int getCheckedPosition() {
        return mCheckedPosition;
    }

    @Override
    public long getAdapterItemId(int position) {
        return position;
    }

    class StickerButtonViewHolder extends RecyclerView.ViewHolder {

        public StickerButtonViewHolder(View itemView) {
            super(itemView);
        }
    }
}
